<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class user_model extends CI_Model {
	private $table = "jonu_user";

	public function __construct(){
		parent::__construct();
		$this->load->library('encrypt');
	}

	public function count_all(){
		$query = $this->db->count_all_results($this->table);
		return $query;
	}
	public function count_verified(){
		$this->db->where('jonu_user_status', 'verified');
		$query = $this->db->count_all_results($this->table);
		return $query;
	}
	public function count_unverified(){
		$this->db->where('jonu_user_status', 'not verified');
		$query = $this->db->count_all_results($this->table);
		return $query;
	}
	public function count_blocked(){
		$this->db->where('jonu_user_status', 'blocked');
		$query = $this->db->count_all_results($this->table);
		return $query;
	}

	public function get_verified(){
		$this->db->where('jonu_user_status', 'verified');
		$query = $this->db->get($this->table);
		$result = $query->result_array();

		return $result;
	}
	public function get_unverified(){
		$this->db->where('jonu_user_status', 'not verified');
		$query = $this->db->get($this->table);
		$result = $query->result_array();

		return $result;
	}
	public function get_blocked(){
		$this->db->where('jonu_user_status', 'blocked');
		$query = $this->db->get($this->table);
		$result = $query->result_array();

		return $result;
	}

	public function get_by_id($id){
		$this->db->where('jonu_user_id', $id);
		$query = $this->db->get($this->table);
		$result = $query->result_array();

		return $result;
	}

	public function login_check($param){
		$this->db->where('jonu_user_username', $param['usr_username']);
		$this->db->where('jonu_user_password', $this->encrypt->sha1($param['usr_pass']));
		$query = $this->db->get($this->table);
		$result = $query->result_array();

		return $result;
	}

	public function store($param){
		$regist_at = date('Y-m-d m:s');
		$status = "not verified";
		$str_key = "jogjanuws";
		$confirm_key = str_shuffle(strtolower($param['usr_username']));
		$confirm_key .= str_shuffle($str_key);
		$confirm_key .= rand(4, 1000);

		$data = array(
			'jonu_user_username'	=> strtolower($param['usr_username']),
			'jonu_user_fullname'	=> $param['usr_fullname'],
			'jonu_user_password'	=> $this->encrypt->sha1($param['usr_pass']),
			'jonu_user_email'		=> $param['usr_email'],
			'jonu_user_sex'			=> $param['usr_jk'],
			'jonu_user_registerat'	=> $regist_at,
			'jonu_user_status'		=> $status,
			'jonu_user_conf_key'	=> $confirm_key
			);
		$do_store = $this->db->insert($this->table, $data);
		
		if($do_store){
			$this->db->select_max('jonu_user_id');
			$query1 = $this->db->get($this->table);
			$result1 = $query1->result_array();

			$this->db->where('jonu_user_id', $result1[0]['jonu_user_id']);
			$query2 = $this->db->get($this->table);
			$result2 = $query2->result_array();
			
			return $result2[0];
		}else{
			return false;
		}		
	}

	public function update_setting($param){
		$usr_id = get_cookie('c_usr_id');
		$data = array(
			'jonu_user_fullname'	=> $param['usr_fullname'],
			'jonu_user_email'		=> $param['usr_email'],
			'jonu_user_sex'			=> $param['usr_jk']
			);
		$this->db->where('jonu_user_id', $usr_id);
		$query = $this->db->update($this->table, $data);

		return $query;
	}

	public function change_pass($param){
		# checking old password
		$this->db->where('jonu_user_id', get_cookie('c_usr_id'));
		$this->db->where('jonu_user_password', $this->encrypt->sha1($param['old_pass']));
		$count_curr_usr = $this->db->count_all_results($this->table);
		if($count_curr_usr == 1){
			$data = array(
				'jonu_user_password' => $this->encrypt->sha1($param['new_pass'])
				);
			$this->db->where('jonu_user_id', get_cookie('c_usr_id'));
			$query = $this->db->update($this->table, $data);

			return $query;
		}else{
			return false;
		}
	}

	public function update_pp($foto){
		$usr_id = get_cookie('c_usr_id');

		#get current photo
		$this->db->where('jonu_user_id', $usr_id);
		$q_get_curr_photo = $this->db->get($this->table);
		$res_get_curr_photo = $q_get_curr_photo->result_array();

		echo $res_get_curr_photo[0]['jonu_user_photo'];
		if($res_get_curr_photo[0]['jonu_user_photo'] != ""){
			$curr_photo_name = $res_get_curr_photo[0]['jonu_user_photo'];
			unlink("./asset/img/auth-foto/$curr_photo_name");
		}

		$data = array(
			'jonu_user_photo'	=> $foto
			);
		$this->db->where('jonu_user_id', $usr_id);
		$query = $this->db->update($this->table, $data);

		return $query;
	}

	public function verify($id, $username, $sec_key){
		$this->db->where('jonu_user_id', $id);
		$this->db->where('jonu_user_username', strtolower($username));
		$this->db->where('jonu_user_conf_key', $sec_key);
		$query = $this->db->get($this->table);
		$result = $query->result_array();

		return $result;	
	}

	public function do_verify($id, $username){
		$data = array(
			'jonu_user_status'	=> 'verified'
			);
		$this->db->where('jonu_user_id', $id);
		$this->db->where('jonu_user_username', strtolower($username));
		$query = $this->db->update($this->table, $data);
		
		return $query;
	}

	public function block($id){
		$data = array('jonu_user_status' => 'blocked');
		$this->db->where('jonu_user_id', $id);
		$query = $this->db->update($this->table, $data);

		return $query;
	}
	public function unblock($id){
		$data = array('jonu_user_status' => 'verified');
		$this->db->where('jonu_user_id', $id);
		$query = $this->db->update($this->table, $data);

		return $query;
	}
}

/* End of file user_model.php */
/* Location: ./application/models/user_model.php */